package com.shh.aiojbackendserver.model.vo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.shh.aiojbackendserver.model.dto.question.JudgeConfig;
import com.shh.aiojbackendserver.model.entity.Question;
import com.shh.aiojbackendserver.model.entity.QuestionAi;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 题目
 * @TableName question_ai
 */
@TableName(value ="question_ai")
@Data
public class QuestionAiVO implements Serializable {
    /**
     * id
     */
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表
     */
    private List<String> tags;

    /**
     * 题目答案
     */
    private String answer;

    /**
     * 判题用例（json 数组）
     */
    private String judgeCase;

    /**
     * 判题配置（json 对象）
     */
    private JudgeConfig judgeConfig;

    /**
     * 包装类转对象 因为VO类中tags 是 List<String> 方便前端使用，但是实体类中是String类型，方便存储到数据库中所以需要进行转换
     *
     * @param questionAiVO
     * @return
     */
    public static QuestionAi voToObj(QuestionAiVO questionAiVO) {
        if (questionAiVO == null) {
            return null;
        }
        QuestionAi questionAi = new QuestionAi();
        BeanUtils.copyProperties(questionAiVO, questionAi);
        List<String> tagList = questionAiVO.getTags();
        if(tagList!= null){
            questionAi.setTags(JSONUtil.toJsonStr(tagList));
        }
        JudgeConfig voJudgeConfig = questionAiVO.getJudgeConfig();
        if(voJudgeConfig != null){
            questionAi.setJudgeConfig(JSONUtil.toJsonStr(voJudgeConfig));
        }
        return questionAi;
    }

    /**
     * 对象转包装类
     *
     * @param questionAi
     * @return
     */
    public static QuestionAiVO objToVo(QuestionAi questionAi) {
        if (questionAi == null) {
            return null;
        }
        QuestionAiVO questionAiVO = new QuestionAiVO();
        BeanUtils.copyProperties(questionAi, questionAiVO);
        List<String> tagList = JSONUtil.toList(questionAi.getTags(),String.class);
        questionAiVO.setTags(tagList);
        //toBean将 JSON 数据转换为 Java 对象。
        JudgeConfig judgeConfig = JSONUtil.toBean(questionAi.getJudgeConfig(),JudgeConfig.class);
        questionAiVO.setJudgeConfig(judgeConfig);
        return questionAiVO;
    }



    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}